package com.woniuxy.creditsystem.systemmag.util;

import com.woniuxy.creditsystem.common.bean.DataDictionaryInfo;
import com.woniuxy.creditsystem.common.bean.ProcessModelInfo;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.CellType;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 导出数据到Excel
 * @Author liaojian
 * */
public class ExportToExcel {
    public static String getFileName(List<DataDictionaryInfo> list) throws Exception{
        //1.创建Excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2.创建工作表对象
        HSSFSheet sheet = workbook.createSheet();
        //3.创建行对象
        int i = 0;
        for (DataDictionaryInfo data:list) {
            HSSFRow row = sheet.createRow(i++);
            //4.创建标题行单元格对象
            HSSFCell cell1 = row.createCell(0);
            HSSFCell cell2 = row.createCell(1);
            HSSFCell cell3 = row.createCell(2);
            HSSFCell cell4 = row.createCell(3);
            HSSFCell cell5 = row.createCell(4);
            HSSFCell cell6 = row.createCell(5);
            //5.给单元格赋值
            cell1.setCellValue(String.valueOf(data.getNumber()));
            cell2.setCellValue(data.getDicType());
            cell3.setCellValue(data.getDicName());
            cell4.setCellValue(String.valueOf(data.getSort()));
            cell5.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(data.getEditTime()));
            cell6.setCellValue(data.getEditPerson());
        }
        //6.创建文件输出流，将数据输出到本地
        String path = "E://files";
        File dir = new File(path);
        //判断该目录是否存在
        if(!dir.exists()){
            dir.mkdir();
        }
        String fileName = path + "/数据字典" + System.currentTimeMillis() + ".xls";
        FileOutputStream fos = new FileOutputStream(fileName);
        workbook.write(fos);
        fos.flush();
        fos.close();
        return fileName;
    }

    public static String getProFileName(List<ProcessModelInfo> list) throws Exception{
        //1.创建Excel对象
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2.创建工作表对象
        HSSFSheet sheet = workbook.createSheet();
        //3.创建行对象
        int i = 0;
        for (ProcessModelInfo pro:list) {
            HSSFRow row = sheet.createRow(i++);
            //4.创建标题行单元格对象
            HSSFCell cell1 = row.createCell(0);
            HSSFCell cell2 = row.createCell(1);
            HSSFCell cell3 = row.createCell(2);
            HSSFCell cell4 = row.createCell(3);
            HSSFCell cell5 = row.createCell(4);
            HSSFCell cell6 = row.createCell(5);
            //5.给单元格赋值
            cell1.setCellValue(String.valueOf(pro.getNum()));
            cell2.setCellValue(pro.getName());
            cell3.setCellValue(pro.getTag());
            cell4.setCellValue(pro.getType());
            cell5.setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(pro.getCreateTime()));
            cell6.setCellValue(pro.getProcessImg());
        }
        //6.创建文件输出流，将数据输出到本地
        String path = "E://files";
        File dir = new File(path);
        //判断该目录是否存在
        if(!dir.exists()){
            dir.mkdir();
        }
        String fileName = path + "/流程模型" + System.currentTimeMillis() + ".xls";
        FileOutputStream fos = new FileOutputStream(fileName);
        workbook.write(fos);
        fos.flush();
        fos.close();
        return fileName;
    }
}
